package com.dong.yi18health;

import java.util.ArrayList;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.TextView;
import fragment.fragment_title1;
import fragment.fragment_title2;
import fragment.fragment_title3;
import fragment.fragment_title4;
import fragment.fragment_title5;
import fragment.fragment_title6;

public class MainActivity extends FragmentActivity implements OnPageChangeListener{
	ArrayList<Fragment> datas=new ArrayList<>();
	private ViewPager vp;
	private LinearLayout ll;
	private HorizontalScrollView scroll;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		ininData();
		initView();
	}

	private void ininData() {
		
		fragment_title1 title1=new fragment_title1();
		fragment_title2 title2=new fragment_title2();
		fragment_title3 title3=new fragment_title3();
		fragment_title4 title4=new fragment_title4();
		fragment_title5 title5=new fragment_title5();
		fragment_title6 title6=new fragment_title6();
		datas.add(title1);datas.add(title2);datas.add(title3);
		datas.add(title4);datas.add(title5);datas.add(title6);
	}

	private void initView() {

		scroll = (HorizontalScrollView) findViewById(R.id.ScrollView1);
		
		vp = (ViewPager) findViewById(R.id.viewPager);
		vp.setAdapter(new A(getSupportFragmentManager()));
		vp.setOnPageChangeListener(this);
		ll = (LinearLayout) findViewById(R.id.title);
		
		vp.setCurrentItem(0);
		change(0);
		
		for (int i = 0; i < ll.getChildCount(); i++) {
			final TextView tv=(TextView) ll.getChildAt(i);
			tv.setTag(i);
			tv.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View arg0) {
					int index = (int) tv.getTag();
					vp.setCurrentItem(index);
					change(index);
				}
			});
		};
	}
	protected void change(int index) {
		for (int i = 0; i < ll.getChildCount(); i++) {
			if(i==index){
				TextView tv=(TextView) ll.getChildAt(i);
				tv.setTextColor(Color.RED);
				
			}else{
				TextView tv = (TextView) ll.getChildAt(i);
				tv.setTextColor(Color.BLACK);
			}
		}
		scroll(index);
	}
	private void scroll(int index) {
//		 int width = getResources().getDisplayMetrics().widthPixels;
		 TextView tv=(TextView) ll.getChildAt(index);
		 int left = tv.getLeft();
		 scroll.smoothScrollTo(left-85, 0);//scroll.scrollto(x,y)没有中间过程            scroll.smoothScrollTo(x, y)有中间过程
		
	}
	class A extends FragmentPagerAdapter{

		public A(FragmentManager fm) {
			super(fm);
			// TODO Auto-generated constructor stub
		}

		@Override
		public Fragment getItem(int arg0) {
			return datas.get(arg0);
		}

		@Override
		public int getCount() {
			return datas.size();
		}
		
	}
	//---------------------------------------
	@Override
	public void onPageScrollStateChanged(int arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onPageSelected(int arg0) {
		change(arg0);
		
	}
}
